package com.alks.function.mapper.produce;

import com.alks.entity.data.entity.PcDrawDetail;
import com.alks.entity.data.entity.PcDrawTemp2;
import com.alks.entity.data.entity.PcStitchBarcodeCollect;
import com.alks.entity.data.entity.PcStitchBarcodeHead;
import com.alks.function.data.dto.PcLotHeadCountDto;
import com.alks.function.data.dto.produce.*;
import com.alks.function.data.request.produce.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface PcStitchBarcodeHeadMapper {

    /**
     * 鞋面条码信息
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "SELECT t.STYLE,t.COLOR,t1.LS_NO,t1.LOT_NO, t1.SIZE_NO, t1.SEQ_NO, t1.CASE_QTY, t1.IN_QTY, t1.OUT_QTY, t1.BARCODE, t1.PRINT_FLAG, t1.PRINT_TIME, t1.DEPT_NAME,t1.DEPT_NO, t1.IN_FLAG, t1.IN_TIME, (t1.CASE_QTY - t1.IN_QTY) as inSum, t1.OUT_FLAG, t1.OUT_TIME, (t1.CASE_QTY - t1.IN_QTY) as outSum,t1.size_seq from PC_LOT_HEAD t left JOIN PC_STITCH_BARCODE_HEAD t1 on t.LOT_NO=t1.LOT_NO ",
            "<where>",
            "AND t1.COMPANY_ID=#{companyId}",
            "<if test='lotNo != null and !lotNo.isEmpty()' >",
            "AND t1.LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='sizeNo != null and !sizeNo.isEmpty()' >",
            "AND t1.SIZE_NO LIKE concat('%', #{sizeNo}, '%')",
            "</if>",
            "<if test='checkBox1 != null and checkBox1 == 1' >",
            "AND t1.PRINT_FLAG is null",
            "</if>",
            "<if test='checkBox1 != null and checkBox2 == 2' >",
            "AND t1.PRINT_FLAG is not null AND t1.PRINT_FLAG = 'Y'",
            "</if>",
            "<if test='checkBox2 != null and checkBox2 == 1' >",
            "AND t1.IN_FLAG is null",
            "</if>",
            "<if test='checkBox2 != null and checkBox2 == 2' >",
            "AND t1.IN_FLAG is not null  AND t1.IN_FLAG = 'Y'",
            "</if>",
            "<if test='checkBox3 != null and checkBox3 == 1' >",
            "AND t1.OUT_FLAG is null",
            "</if>",
            "<if test='checkBox3 != null and checkBox3 == 2' >",
            "AND t1.OUT_FLAG is not null  AND t1.OUT_FLAG = 'Y'",
            "</if>",
            "<if test='checkBox4 != null and checkBox4== 1' >",
            "AND (t1.CASE_QTY - t1.IN_QTY) !=0",
            "</if>",
            "<if test='checkBox5 != null and checkBox5 == 1' >",
            "AND (t1.CASE_QTY-t1.IN_QTY) !=0",
            "</if>",
            "<if test='barcode != null and !barcode.isEmpty()' >",
            "AND t1.barcode = #{barcode}",
            "</if>",
            "</where>",
            "</script>",
    })
    List<PcStitchBarcodeHeadListDto> getPcStitchBarcodeHeadList(PcStitchBarcodeHeadListRequest request);

    /**
     * 统计收发料总数量
     * @param request
     * @return
     */

    @Select({
            "<script>",
            "SELECT SUM(t1.IN_QTY) as sunInQty,SUM(t1.OUT_QTY) as sunOutQty from PC_LOT_HEAD t left JOIN PC_STITCH_BARCODE_HEAD t1 on t.LOT_NO=t1.LOT_NO ",
            "<where>",
            "AND t1.COMPANY_ID=#{companyId}",
            "<if test='lotNo != null and !lotNo.isEmpty()' >",
            "AND t1.LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='sizeNo != null and !sizeNo.isEmpty()' >",
            "AND t1.SIZE_NO LIKE concat('%', #{sizeNo}, '%')",
            "</if>",
            "<if test='checkBox1 != null and checkBox1 == 1' >",
            "AND t1.PRINT_FLAG is null",
            "</if>",
            "<if test='checkBox2 != null and checkBox2 == 2' >",
            "AND t1.PRINT_FLAG is not null",
            "</if>",
            "<if test='checkBox2 != null and checkBox2 == 1' >",
            "AND t1.IN_FLAG is null",
            "</if>",
            "<if test='checkBox3 != null and checkBox3 == 1' >",
            "AND t1.OUT_FLAG is null",
            "</if>",
            "<if test='checkBox3 != null and checkBox3 == 2' >",
            "AND t1.OUT_FLAG is not null",
            "</if>",
            "<if test='checkBox4 != null and checkBox4== 1' >",
            "AND (t1.CASE_QTY - t1.IN_QTY) !=0",
            "</if>",
            "<if test='checkBox5 != null and checkBox5 == 1' >",
            "AND (t1.CASE_QTY-t1.IN_QTY) !=0",
            "</if>",
            "<if test='barcode != null and !barcode.isEmpty()' >",
            "AND t1.barcode = #{barcode}",
            "</if>",
            "</where>",
            "</script>",
        })
    PcLotHeadCountDto getPcStitchBarcodeHeadListTotalQuantityofReceivedReceivedMaterials(PcStitchBarcodeHeadListRequest request);

    /*根据编码查询数据*/
    PcStitchBarcodeHeadDto getPcStitchBarcodeHead(String companyId,String barcode);

    /**
     * 统计入库尾数和出库尾数
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "select sum(CASE_QTY-IN_QTY) as orderCount,sum(CASE_QTY-IN_QTY) as surplusCount from PC_STITCH_BARCODE_HEAD",
            "<where>",
            "AND COMPANY_ID=#{companyId}",
            "<if test='lotNo != null and !lotNo.isEmpty()' >",
            "AND LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='sizeNo != null and !sizeNo.isEmpty()' >",
            "AND SIZE_NO LIKE concat('%', #{sizeNo}, '%')",
            "</if>",
            "<if test='checkBox1 != null and checkBox1 ==1' >",
            "AND PRINT_FLAG is null",
            "</if>",
            "<if test='checkBox4 != null and checkBox4 ==1' >",
            "AND (CASE_QTY - IN_QTY) !=0",
            "</if>",
            "<if test='checkBox5 != null and checkBox5 ==1' >",
            "AND (CASE_QTY-IN_QTY) !=0",
            "</if>",
            "</where>",
            "</script>",
    })
    PcLotHeadCountDto getPcStitchBarcodeHeadCount(PcStitchBarcodeHeadListRequest request);

    /**
     * 统计总数量
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "select sum(CASE_QTY) from PC_STITCH_BARCODE_HEAD",
            "<where>",
            "AND COMPANY_ID=#{companyId}",
            "<if test='lotNo != null and !lotNo.isEmpty()' >",
            "AND LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='sizeNo != null and !sizeNo.isEmpty()' >",
            "AND SIZE_NO LIKE concat('%', #{sizeNo}, '%')",
            "</if>",
            "<if test='checkBox1 != null and checkBox1 ==1' >",
            "AND PRINT_FLAG is null",
            "</if>",
            "<if test='checkBox4 != null and checkBox4 ==1' >",
            "AND (CASE_QTY - IN_QTY) !=0",
            "</if>",
            "<if test='checkBox5 != null and checkBox5 ==1' >",
            "AND (CASE_QTY-IN_QTY) !=0",
            "</if>",
            "</where>",
            "</script>",
    })
    Integer getPcStitchBarcodeHeadCaseQtyCount(PcStitchBarcodeHeadListRequest request);

    /**
     * 查询是否存在扫描数据
     * @param lotNo
     * @param companyId
     * @return
     */
    @Select("select count(1) from PC_STITCH_BARCODE_COLLECT where LOT_NO=#{lotNo} AND COMPANY_ID=#{companyId}")
    Integer queryCollectStitchByLotNo(@Param("lotNo") String lotNo, @Param("companyId") String companyId);

    /**
     * 查询条码是否入库
     * @param barcode
     * @param companyId
     * @return
     */
    @Select("select count(1) from PC_STITCH_BARCODE_COLLECT where BARCODE=#{barcode} AND COMPANY_ID=#{companyId}")
    Integer queryCollectStitchByBarcode(@Param("barcode") String barcode, @Param("companyId") String companyId);

    /**
     * 查询指令是否存在
     *
     * @param lotNo
     * @param companyId
     */
    @Select("select count(1) from PC_STITCH_BARCODE_HEAD where LOT_NO=#{lotNo} AND COMPANY_ID=#{companyId}")
    Integer getPcStitchBarcodeHeadExist(@Param("lotNo") String lotNo, @Param("companyId") String companyId);

    @Select("select count(1) from PC_STITCH_BARCODE_HEAD where LOT_NO=#{lotNo} AND COMPANY_ID=#{companyId} and LS_NO=#{lsNo}")
    Integer getPcStitchBarcodeHeadExist2(@Param("lotNo") String lotNo, @Param("companyId") String companyId,@Param("lsNo") int lsNo);
    /**
     * 新增鞋面标签数据
     * @param list
     */
    @Insert({
            "<script>",
            "INSERT INTO PC_STITCH_BARCODE_HEAD",
            "(COMPANY_ID, LOT_NO, LS_NO, SIZE_NO, SEQ_NO, CASE_QTY, BARCODE, PRINT_FLAG, PRINT_TIME, IN_FLAG, OUT_FLAG, IN_QTY, OUT_QTY, IN_TIME, OUT_TIME, SIZE_SEQ, PER_BAG_QTY, DEPT_NAME, RE_PRINT_FLAG)",
            "VALUES",
            "<foreach collection='list' item='t' index='index' separator=','>",
            "(#{t.companyId}, #{t.lotNo}, #{t.lsNo}, #{t.sizeNo}, #{t.seqNo}, #{t.caseQty}, #{t.barcode}, #{t.printFlag}, #{t.printTime}, #{t.inFlag}, #{t.outFlag}, #{t.inQty}, #{t.outQty}, #{t.inTime}, #{t.outTime}, #{t.sizeSeq}, #{t.perBagQty}, #{t.deptName}, #{t.rePrintFlag})",
            "</foreach>",
            "</script>"
    })
    int addPcStitchBarcodeHead(@Param("list") List<PcStitchBarcodeHead> list);

    /**
     * 删除指令标签
     * @param lotNo
     * @param companyId
     * @return
     */
    @Delete("delete from PC_STITCH_BARCODE_HEAD where COMPANY_ID = #{companyId} and LOT_NO = #{lotNo}")
    int delete(@Param("lotNo") String lotNo, @Param("companyId") String companyId);

    /**
     * 打印所选标签
     *
     * @param barcodeList 条形码集合
     * @param deptName    部门
     * @param now         时间
     * @param flag        检核状态
     * @param companyId
     */
    @Update({
            "<script>",
            "update PC_STITCH_BARCODE_HEAD set PRINT_FLAG=#{flag},PRINT_TIME=#{now},DEPT_NAME=#{deptName},DEPT_NO=#{deptNo} where COMPANY_ID=#{companyId} and BARCODE in"+
            "<foreach collection='barcodeList' item='barcode' separator=',' open='(' close=')'>"+
            "#{barcode}"+
            "</foreach>",
            "</script>",
    })
    void updatePcStitchBarcodeHead(@Param("barcodeList") List<String> barcodeList,
                                   @Param("deptName") String deptName,
                                   @Param("deptNo") String deptNo,
                                   @Param("now") LocalDateTime now,
                                   @Param("flag") String flag,
                                   @Param("companyId") String companyId);

    /**
     * 查询要打印的标签
     *
     * @param lotNo
     * @param sizeNoList
     * @param companyId
     * @return
     */
    @Select({
            "<script>",
            "select BARCODE from PC_STITCH_BARCODE_HEAD where LOT_NO=#{lotNo} and COMPANY_ID=#{companyId} AND IN_FLAG IS NULL and  SIZE_NO in "+
                    "<foreach collection='sizeNoList' item='sizeNoList' separator=',' open='(' close=')'>"+
                    "#{sizeNoList}"+
                    "</foreach>",
            "</script>",
    })
    List<String> getPcStitchBarcodeHeadList2(@Param("lotNo") String lotNo, @Param("sizeNoList") List<String> sizeNoList, @Param("companyId") String companyId);

    /**
     * 查询财务是否已经封账处理
     * @param lotNo
     * @param companyId
     */
    @Select("SELECT COUNT(1) > 0 FROM PC_COST_LOT WHERE LOT_NO = #{lotNo} AND CFM_FLAG = 'Y' and COMPANY_ID=#{companyId}")
    Integer getPcCostLot(String lotNo, String companyId);

    /**
     * 指令用量查询
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "SELECT t2.LOT_NO, t2.partName, t2.MTL_NAME, t2.MTL_COLOR, t2.MTL_NO, t2.MTL_SPEC, t2.MTL_UNIT, t2.ttlUsage, COALESCE(t2.STK_QTY,0) stkQty,CASE WHEN t3.LOT_NO IS NOT NULL THEN '0' WHEN t4.LOT_NO IS NOT NULL THEN '1'ELSE null END AS status," +
                    "COALESCE(t4.ACT_QTY,0) actQty,COALESCE(t2.ttlUsage, 0)-COALESCE(t4.ACT_QTY, 0) as notActQty,COALESCE(t4.REQ_QTY,0) reqQty,COALESCE(t2.ttlUsage, 0)-COALESCE(t4.REQ_QTY, 0) as noReqQTY " +
            "FROM(SELECT t.COMPANY_ID,t.LOT_NO, t.MTL_NAME, t.MTL_COLOR, t.MTL_NO, t.MTL_SPEC, t.MTL_UNIT, t.ttlUsage, t.partName, t1.STK_QTY FROM(SELECT COMPANY_ID,LOT_NO, MTL_NAME, MTL_COLOR, MTL_NO, MTL_SPEC, MTL_UNIT, SUM(TTL_USAGE) AS ttlUsage, GROUP_CONCAT(PART_NAME SEPARATOR '/') AS partName FROM PC_LOT_USAGE " +
            "WHERE  TTL_USAGE IS NOT NULL GROUP BY LOT_NO, MTL_NO,COMPANY_ID) t LEFT JOIN PC_MTL_ACC t1 ON t.MTL_NAME = t1.MTL_NAME and  COALESCE(t.MTL_COLOR, '') = COALESCE(t1.MTL_COLOR, '') AND COALESCE(t.MTL_SPEC, '') = COALESCE(t1.MTL_SPEC, '') and t.MTL_UNIT = t1.MTL_UNIT and t.COMPANY_ID=t1.COMPANY_ID)" +
                    " t2 LEFT JOIN PC_DRAW_TEMP2 t3 ON t2.LOT_NO = t3.LOT_NO AND t2.MTL_NO = t3.MTL_NO and t2.COMPANY_ID=t3.COMPANY_ID LEFT JOIN " +
                    "(SELECT COMPANY_ID,LOT_NO,MTL_NO,sum(ACT_QTY) as ACT_QTY ,sum(REQ_QTY) as REQ_QTY from PC_DRAW_DETAIL GROUP BY LOT_NO,MTL_NO,COMPANY_ID) t4 ON t2.LOT_NO = t4.LOT_NO AND t2.MTL_NO = t4.MTL_NO and t2.COMPANY_ID=t4.COMPANY_ID ",
            "<where>",
            "NOT EXISTS (SELECT 1 FROM PC_COST_LOT WHERE LOT_NO = t2.LOT_NO AND CFM_FLAG = 'Y' and COMPANY_ID=#{companyId}) and t2.COMPANY_ID=#{companyId}",
            "<if test='lotNo != null and !lotNo.isEmpty()' >",
            "AND t2.LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='typeNo != null and !typeNo.isEmpty()' >",
            "AND SUBSTRING(t2.MTL_NO, 1, 2)=#{typeNo} ",
            "</if>",
            "<if test='mtlName != null and !mtlName.isEmpty()' >",
            "AND t2.MTL_NAME LIKE concat('%', #{mtlName}, '%')",
            "</if>",
            "<if test='status != null and status== 1' >",
            "AND COALESCE(t4.ACT_QTY, 0)>0",
            "</if>",
            "<if test='status != null and status== 0' >",
            "AND COALESCE(t2.ttlUsage, 0) - COALESCE(t4.ACT_QTY, 0) >0",
            "</if>",
            "</where>",
            "</script>",
    })
    List<PcLotUsageToPcDrawDetailDto> QueryOfInstructionUsageDetails(LotNoQueryRequest request);

    /**
     * 根据用户查询待确认领料用量明细
     *
     * @param loginName
     * @param slipType  领料类型
     * @param companyId
     */
    @Select("SELECT DISTINCT t.LOT_NO,t.MTL_NO,t.MTL_COLOR,t.MTL_NAME,t.TTL_USAGE,t1.STK_QTY,t.REQ_QTY,t.REMARK,t.MTL_SPEC,t.MTL_UNIT,t2.ACT_QTY," +
            "COALESCE(t.TTL_USAGE, 0)-COALESCE(t.ACT_QTY, 0)  as notActQty,t2.REQ_QTY as reqQty2,t.SLIP_TYPE,t.SUPPLEMENT_NO" +
            " from PC_DRAW_TEMP2 t LEFT JOIN PC_MTL_ACC t1 on t.MTL_NO=t1.MTL_NO and t.COMPANY_ID=t1.COMPANY_ID" +
            " LEFT JOIN (SELECT LOT_NO,MTL_NO,sum(ACT_QTY) as ACT_QTY ,sum(REQ_QTY) as REQ_QTY from PC_DRAW_DETAIL GROUP BY LOT_NO,MTL_NO)" +
            " t2 ON t2.LOT_NO = t.LOT_NO AND t2.MTL_NO = t.MTL_NO" +
            " where t.SYS_USER=#{loginName} and t.SLIP_TYPE=#{slipType} AND t.COMPANY_ID=#{companyId}")
    List<PcDrawTemp2Dto> QueryOfInstructionUsageDetails2(String loginName, String slipType, String companyId);

    /**
     * 领料明细档查询
     * @param request
     * @param loginName
     * @param slipType 领料类型
     * @return
     */
    @Select({
            "<script>",
            "SELECT t.SLIP_NO,t.SLIP_DATE,t.LOT_NO,t.MTL_NO,t.MTL_COLOR,t.MTL_NAME,t.REQ_QTY,t1.STK_QTY,t.REMARK,t.MTL_SPEC,t.MTL_UNIT,t.ACT_QTY,t2.ttlUsage," +
            "COALESCE(t2.ttlUsage,0)-COALESCE(t.ACT_QTY,0) as reqQty2,t.SYS_USER,t.PRN_FLAG,t.SLIP_TYPE,t.SUPPLEMENT_NO,t3.ORDER_QTY,t3.STYLE from PC_DRAW_DETAIL t LEFT JOIN  PC_MTL_ACC t1 on t.MTL_NO=t1.MTL_NO and t.COMPANY_ID=t1.COMPANY_ID " +
            "LEFT JOIN(SELECT SUM(TTL_USAGE) AS ttlUsage,MTL_NO,LOT_NO,COMPANY_ID FROM PC_LOT_USAGE WHERE TTL_USAGE IS NOT NULL GROUP BY LOT_NO, MTL_NO,COMPANY_ID)t2 " +
            "on t2.LOT_NO = t.LOT_NO AND t2.MTL_NO = t.MTL_NO LEFT JOIN PC_LOT_HEAD t3 on t3.LOT_NO=t.LOT_NO and  t3.COMPANY_ID=t1.COMPANY_ID",
            "<where>",
            "t.COMPANY_ID=#{r.companyId}",
            "<if test='r.slipNo != null and !r.slipNo.isEmpty()' >",
            "and t.SLIP_NO LIKE concat('%', #{r.slipNo}, '%')",
            "</if>",
            "<if test='r.lotNo != null and !r.lotNo.isEmpty()' >",
            "AND t.LOT_NO LIKE concat('%', #{r.lotNo}, '%')",
            "</if>",
            "<if test='r.slipDateStart != null and r.slipDateEnd != null' >",
            "AND t.SLIP_DATE BETWEEN #{r.slipDateStart} AND #{r.slipDateEnd}",
            "</if>",
            "<if test='r.slipDateStart != null' >",
            "AND t.SLIP_DATE &gt;= #{r.slipDateStart}",
            "</if>",
            "<if test='r.slipDateEnd != null' >",
            "AND t.SLIP_DATE &lt;= #{r.slipDateEnd}",
            "</if>",
            "<if test='loginName != null and !loginName.isEmpty()' >",
            "and t.SYS_USER=#{loginName}",
            "</if>",
            "<if test='slipType != null and !slipType.isEmpty()' >",
            "and t.SLIP_TYPE =#{slipType}",
            "</if>",
            "<if test='r.supplementNo != null and !r.supplementNo.isEmpty()' >",
            "AND t.SUPPLEMENT_NO LIKE concat('%', #{r.supplementNo}, '%')",
            "</if>",
            "</where>",
            "</script>",
    })
    List<PcDrawDetailDto> MaterialRequisitionDetailsFile(@Param("r") PcDrawDetailRequest request,@Param("loginName") String loginName,@Param("slipType") String slipType);

    /**
     * 新增待领批处理
     * @param list
     */
    @Insert({
            "<script>",
            "INSERT INTO PC_DRAW_TEMP2 (COMPANY_ID, LOT_NO, MTL_NO, MTL_NAME, MTL_COLOR, MTL_SPEC, MTL_UNIT, TTL_USAGE, SYS_DATE, SYS_USER, SLIP_TYPE, REMARK, SUPPLEMENT_NO, REQ_QTY, ACT_QTY) " +
            "VALUES " ,
            "<foreach collection='list' item='t' separator=','>",
            "(#{t.companyId}, #{t.lotNo}, #{t.mtlNo}, #{t.mtlName}, #{t.mtlColor}, #{t.mtlSpec}, #{t.mtlUnit}, #{t.ttlUsage}, #{t.sysDate}, #{t.sysUser}, #{t.slipType}, #{t.remark}, #{t.supplementNo}, #{t.reqQty}, #{t.actQty})",
            "</foreach>",
            "</script>",
    })
    void addPcDrawTemp2(@Param("list") List<PcDrawTemp2> list);

    /**
     * 新增领料明细
     * @param list
     */
    @Insert({
            "<script>",
            "INSERT INTO PC_DRAW_DETAIL (COMPANY_ID, STOCK_NO, SLIP_NO, LOT_NO, MTL_NO, MTL_NAME, MTL_COLOR, MTL_SPEC, MTL_UNIT, REQ_QTY, ACT_QTY, SLIP_DATE, DEPT_NO, DEPT_NAME, SYS_DATE, SYS_USER, SLIP_TYPE, SUPPLEMENT_NO, REMARK, CANCEL_FLAG, CANCEL_DATE, CANCEL_USER, PRN_FLAG, PRINT_TIME, PRINT_DATE, CFM_FLAG, CFM_DATE, CFM_USER, DRAW_DEPT) " +
                    "VALUES ",
            "<foreach collection='list' item='t' separator=','>",
            "(#{t.companyId}, #{t.stockNo}, #{t.slipNo}, #{t.lotNo}, #{t.mtlNo}, #{t.mtlName}, #{t.mtlColor}, #{t.mtlSpec}, #{t.mtlUnit}, #{t.reqQty}, #{t.actQty}, #{t.slipDate}, #{t.deptNo}, #{t.deptName}, #{t.sysDate}, #{t.sysUser}, #{t.slipType}, #{t.supplementNo}, #{t.remark}, #{t.cancelFlag}, #{t.cancelDate}, #{t.cancelUser}, #{t.prnFlag}, #{t.printTime}, #{t.printDate}, #{t.cfmFlag}, #{t.cfmDate}, #{t.cfmUser}, #{t.drawDept})",
            "</foreach>",
            "</script>"
    })
    void addPcDrawDetail(@Param("list") List<PcDrawDetail> list);

    /**
     * 删除待批处理数据
     * @param request
     * @param slipType 领料类型
     */
    @Delete({
            "<script>",
            "<foreach collection='request' item='t' separator=';'>",
            "delete from PC_DRAW_TEMP2 where",
            "LOT_NO=#{t.lotNo} and MTL_NO=#{t.mtlNo} and SLIP_TYPE=#{slipType}",
            "</foreach>",
            "</script>"
    })
    void delPcDrawTemp2(@Param("request") List<PcDrawDetailAddRequest> request,@Param("slipType") String slipType);

    /**
     * 更新打印时间
     * @param request
     * @param printFlag 打印状态
     * @param printDate 打印时间
     */
    @Update({
            "<script>",
            "<foreach collection='list' item='t' index='index' separator=';'>",
            "update PC_DRAW_DETAIL set PRINT_TIME=#{t.printTime}, PRN_FLAG=#{printFlag}, PRINT_DATE=#{printDate} where LOT_NO=#{t.lotNo} and SLIP_NO=#{t.slipNo} and MTL_NO=#{t.mtlNo}",
            "</foreach>",
            "</script>"
    })
    void updatePcDrawDetailByPrintTime(@Param("list") List<PcDrawDetailUpdateRequest> request,@Param("printFlag") String printFlag,@Param("printDate") LocalDateTime printDate);

    /**
     * 修改备注
     * @param lotNo 指令
     * @param mtlNo 物料编码
     * @param slipNo 备注
     */
    @Update("update PC_DRAW_TEMP2 set REMARK=#{slipNo}  where LOT_NO=#{lotNo} and MTL_NO=#{mtlNo} and SLIP_TYPE=#{slipType}")
    void updatePcDrawTemp2(@Param("lotNo") String lotNo,@Param("mtlNo") String mtlNo,@Param("slipNo") String slipNo,@Param("slipType") String slipType);

    /**
     * 部位用量明细查询
     * @param lotNo 指令
     * @param mtlNo 物料编码
     */
    @Select("select PART_NAME as partName,TTL_USAGE as ttlUsage   from PC_LOT_USAGE where LOT_NO=#{lotNo} and MTL_NO=#{mtlNo}")
    List<Map<String,String>> getPartNameList(@Param("lotNo") String lotNo,@Param("mtlNo") String mtlNo);

    /**
     * 查询预加料
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "SELECT t2.LOT_NO, t2.partName, t2.MTL_NAME, t2.MTL_COLOR, t2.MTL_NO, t2.MTL_SPEC, t2.MTL_UNIT, t2.ttlUsage, t2.STK_QTY,CASE WHEN t3.LOT_NO IS NOT NULL THEN '0' WHEN t4.LOT_NO IS NOT NULL THEN '1'ELSE null END AS status,COALESCE(t4.ACT_QTY,0) actQty,COALESCE(t2.ttlUsage,0)-COALESCE(t4.ACT_QTY,0) as notActQty,COALESCE(t4.REQ_QTY,0) reqQty,COALESCE(t2.ttlUsage,0)-COALESCE(t4.REQ_QTY,0) as noReqQTY,COALESCE(t2.addUsage,0) addUsage " +
                    "FROM(SELECT t.COMPANY_ID,t.LOT_NO, t.MTL_NAME, t.MTL_COLOR, t.MTL_NO, t.MTL_SPEC, t.MTL_UNIT, t.ttlUsage, t.partName, t1.STK_QTY,t.addUsage FROM(SELECT COMPANY_ID,LOT_NO, MTL_NAME, MTL_COLOR, MTL_NO, MTL_SPEC, MTL_UNIT, SUM(TTL_USAGE) AS ttlUsage, GROUP_CONCAT(PART_NAME SEPARATOR '/') AS partName,SUM(ADD_USAGE) AS addUsage FROM PC_LOT_USAGE " +
                    "WHERE  TTL_USAGE IS NOT NULL GROUP BY LOT_NO, MTL_NO,COMPANY_ID) t LEFT JOIN PC_MTL_ACC t1 ON t.MTL_NO = t1.MTL_NO and t.COMPANY_ID=t1.COMPANY_ID) t2 LEFT JOIN PC_DRAW_TEMP2 t3 ON t2.LOT_NO = t3.LOT_NO AND t2.MTL_NO = t3.MTL_NO and t2.COMPANY_ID=t3.COMPANY_ID LEFT JOIN (SELECT COMPANY_ID,LOT_NO,MTL_NO,sum(ACT_QTY) as ACT_QTY ,sum(REQ_QTY) as REQ_QTY from PC_DRAW_DETAIL GROUP BY LOT_NO,MTL_NO,COMPANY_ID) t4 ON t2.LOT_NO = t4.LOT_NO AND t2.MTL_NO = t4.MTL_NO and t2.COMPANY_ID=t4.COMPANY_ID",
            "<where>",
            "NOT EXISTS (SELECT 1 FROM PC_COST_LOT WHERE LOT_NO = t2.LOT_NO AND CFM_FLAG = 'Y' AND COMPANY_ID=#{companyId}) and t2.addUsage is not null and t2.COMPANY_ID=#{companyId}",
            "<if test='lotNo != null and !lotNo.isEmpty()' >",
            "AND t2.LOT_NO LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='typeNo != null and !typeNo.isEmpty()' >",
            "AND SUBSTRING(t2.MTL_NO, 1, 2)=#{typeNo} ",
            "</if>",
            "<if test='mtlName != null and !mtlName.isEmpty()' >",
            "AND t2.MTL_NAME LIKE concat('%', #{mtlName}, '%')",
            "</if>",
            "<if test='status != null and status== 1' >",
            "AND COALESCE(t4.ACT_QTY, 0)>0",
            "</if>",
            "<if test='status != null and status== 0' >",
            "AND COALESCE(t2.ttlUsage, 0) - COALESCE(t4.ACT_QTY, 0) >0",
            "</if>",
            "</where>",
            "</script>",
    })
    List<PcLotUsageToPcDrawDetailDto> SelectPreAddedMaterials(LotNoQueryRequest request);

    /**
     * 查询补料单
     * @param request
     * @return
     */
    @Select({
            "<script>",
            "SELECT t. SLIP_NO,t1.REMARK AS lotNo,t1.MTL_NAME,t1.MTL_COLOR,t1.MTL_SPEC,t1.MTL_NO,t1.MTL_UNIT,t1.ACT_QTY1 as ttlUsage,COALESCE(t2.STK_QTY,0) stkQty,t.PRN_FLAG,COALESCE(t4.ACT_QTY,0) actQty,COALESCE(ACT_QTY1,0)-COALESCE(t4.ACT_QTY,0) as notActQty,CASE WHEN t3.LOT_NO IS NOT NULL THEN '0' WHEN t4.LOT_NO IS not  NULL THEN '1' ELSE null END AS status from PC_SUPPLEMENT_HEAD t " +
                    " LEFT JOIN PC_SUPPLEMENT_MTL t1 ON t.SLIP_NO=t1.SLIP_NO LEFT JOIN PC_MTL_ACC t2 on t1.MTL_NO=t2.MTL_NO and t1.COMPANY_ID=t2.COMPANY_ID  LEFT JOIN PC_DRAW_TEMP2 t3 ON t1.REMARK = t3.LOT_NO AND t1.MTL_NO = t3.MTL_NO and t.SLIP_NO=t3.SUPPLEMENT_NO and t3.COMPANY_ID=t.COMPANY_ID LEFT JOIN (SELECT LOT_NO,MTL_NO,sum(ACT_QTY) as ACT_QTY,SLIP_TYPE,SUPPLEMENT_NO,COMPANY_ID from PC_DRAW_DETAIL where SLIP_TYPE='补料领料' GROUP BY LOT_NO,MTL_NO,SLIP_TYPE,SUPPLEMENT_NO,COMPANY_ID)" +
                    " t4 ON t1.REMARK = t4.LOT_NO AND t1.MTL_NO = t4.MTL_NO and t.SLIP_NO=t4.SUPPLEMENT_NO and t.COMPANY_ID=t4.COMPANY_ID",
            "<where>",
            " t.PRN_FLAG='Y' and t.COMPANY_ID=#{companyId}",
            "<if test='lotNo != null and !lotNo.isEmpty()' >",
            "AND t1.REMARK LIKE concat('%', #{lotNo}, '%')",
            "</if>",
            "<if test='typeNo != null and !typeNo.isEmpty()' >",
            "AND SUBSTRING(t1.MTL_NO, 1, 2)=#{typeNo} ",
            "</if>",
            "<if test='mtlName != null and !mtlName.isEmpty()' >",
            "AND t1.MTL_NAME LIKE concat('%', #{mtlName}, '%')",
            "</if>",
            "<if test='status != null and status== 1' >",
            "AND COALESCE(t4.ACT_QTY, 0)>0",
            "</if>",
            "<if test='status != null and status== 0' >",
            "AND COALESCE(ACT_QTY1, 0) - COALESCE(t4.ACT_QTY, 0) >0",
            "</if>",
            "<if test='slipNo != null and !slipNo.isEmpty()' >",
            "AND t.SLIP_NO LIKE concat('%', #{slipNo}, '%')",
            "</if>",
            "</where>",
            "</script>",
    })
    List<PcLotUsageToPcDrawDetailDto> SelectSupplementaryOrderMaterials(LotNoQueryRequest request);

    /**
     * 打印单据
     * @param slipNo
     * @param printTime
     * @param flag
     * @param printDate
     */
    @Update("update PC_DRAW_DETAIL set PRINT_TIME=#{printTime},PRN_FLAG=#{flag},PRINT_DATE=#{printDate} where  SLIP_NO=#{slipNo}")
    void updatePcDrawDetailByPrintTime2(@Param("slipNo") String slipNo,@Param("printTime") BigDecimal printTime,@Param("flag") String flag,@Param("printDate") LocalDateTime printDate);

    /*扫描后更新数据*/
    int updatePcStitchBarcodeHeadByScan(PcStitchBarcodeDto stitchDto);

    /*加入收集更细表*/
    int insertPcStitchBarcodeCollect(PcStitchBarcodeDto stitchDto);

    /*当前缴库单位没有打单据的指令及其号码数量*/
    List<PcStitchBarcodeCollect> queryCollectStitch(StitchScanRequest request);

    /*按照缴库+指令+号码分组查询未打印单据的*/
    List<StitchCollectPrintDto> queryCollectStitchOfDepetALotASize(StitchScanRequest request);

    /*按照缴库+指令查询未打印单据*/
    List<StitchCollectPrint2Dto> queryCollectStitchOfDepetALot(StitchScanRequest request);

    /*按照指令分组查询*/
    List<StitchCollectPrintDto> queryCollectStitchGroupLot(PcStitchBarcodeDto request);

    /*批量更新状态*/
    int updatePcStitchBarcodeCollect(@Param("request") StitchScanRequest request, @Param("now") LocalDateTime now);

    /*该部门查询该指令未打印单据的总数*/
    PcStitchBarcodeDto queryCollectStitchByLot(PcStitchBarcodeDto stitchDto);

    /*查询是否扫描过*/
    PcStitchBarcodeCollect getPcStitchBarcodeCollect(StitchScanRequest request);

    @Select({
            "<script>",
            "SELECT t.STYLE,t.COLOR,t1.LOT_NO, t1.SIZE_NO, t1.SEQ_NO, t1.CASE_QTY, t1.IN_QTY, t1.OUT_QTY, t1.BARCODE, t1.PRINT_FLAG, t1.PRINT_TIME, t1.DEPT_NAME,t1.DEPT_NO, t1.IN_FLAG, t1.IN_TIME, (t1.CASE_QTY - t1.IN_QTY) as inSum, t1.OUT_FLAG, t1.OUT_TIME, (t1.CASE_QTY - t1.IN_QTY) as outSum,t1.LS_NO from PC_LOT_HEAD t left JOIN PC_STITCH_BARCODE_HEAD t1 on t.LOT_NO=t1.LOT_NO ",
            "where",
            " t1.COMPANY_ID=#{companyId}",
            "AND t1.LOT_NO=#{lotNo} ",
            "AND t1.SIZE_NO in (",
            "<foreach item='barcode' collection='barcodeList' open='' separator=',' close=''>",
            "#{barcode} ",
            "</foreach>",
            ")",
            "</script>",
    })
    List<PcStitchBarcodeHeadListDto> printPcStitchBarcodeHeadList(@Param("lotNo") String lotNo, @Param("barcodeList") List<String> barcodeList, @Param("companyId") String companyId);

    /**
     * 查询未入库的标签信息
     *
     * @param companyId
     * @param lotNo
     * @param lsNo
     * @return
     */
    List<PcStitchBarcodeHead> selectUnStartBarcode(@Param("companyId") String companyId, @Param("lotNo") String lotNo, @Param("lsNo") int lsNo);

    /**
     * 查询号码最大序号
     * @param lotNo
     * @param companyId
     * @param sizeNo
     * @return
     */
    int pcStitchBarcodeHeadMapper(@Param("lotNo") String lotNo, @Param("companyId") String companyId, @Param("sizeNo") String sizeNo);

    /**
     * 查询重新生成的号码数量是否与指令数量相等
     * @param lotNo
     * @param companyId
     * @return
     */
    int countNoEqual(@Param("lotNo") String lotNo, @Param("companyId") String companyId);

    /**
     * 删除号码标签数据
     * @param batchList
     * @return
     */
    int deleteBarcodes(@Param("batchList") List<String> batchList,@Param("companyId") String companyId);

    /**
     * 查询重新生成的号码数量
     * @param lotNo
     * @param companyId
     * @return
     */
    List<Map<String, Object>> selectRegenerateSize(@Param("lotNo") String lotNo, @Param("companyId") String companyId);

    /**
     * 根据条码查询条码信息
     * @param barcode
     * @return
     */
    PcStitchBarcodeHead selectByBarcode(@Param("barcode") String barcode, @Param("companyId") String companyId);

    /**
     * 更新条码是否重复打印标识
     * @param sbh
     * @return
     */
    int updateRePrintFlagByBarcode(@Param("t") PcStitchBarcodeHead sbh, @Param("barcode") String barcode);

    /**
     * 查询标签是否入库
     * @param barcode
     * @param companyId
     * @return
     */
    PcStitchBarcodeHead checkCheckLabelByBarcode(@Param("barcode") String barcode, @Param("companyId") String companyId);

    /**
     * 删除标签
     * @param companyId
     * @param barcode
     * @return
     */
    int deleteBarcode(@Param("companyId") String companyId, @Param("barcode") String barcode);


    /*统计 PC_STITCH_BARCODE_COLLECT  当前指令+号码+出库/入库的数量*/
    PcStitchBarcodeCollect getPcStitchBarcodeCollectQty(PcStitchBarcodeDto request);


    /*统计  PC_STITCH_BARCODE_COLLECT  当前指令+号码+出库/入库 + 部门的数量*/
    PcStitchBarcodeCollect getPcStitchBarcodeCollectQty2(PcStitchBarcodeDto stitchDto);

    /**
     * 查询多购
     * @param request
     * @return
     */
    List<PcLotUsageToPcDrawDetailDto> moreList(LotNoQueryRequest request);

    /**
     * 查询多购
     *
     * @param request
     * @param companyId
     * @return
     */
    PcLotUsageToPcDrawDetailDto moreList2(@Param("r") PcDrawDetailAddRequest request, @Param("companyId") String companyId);

    /**
     * 多购领料
     * @param request
     * @param loginName
     * @param slipType
     * @return
     */
    List<PcDrawDetailDto> moreList3(@Param("r") PcDrawDetailRequest request, @Param("loginName") String loginName, @Param("slipType") String slipType);
}
